package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.pojo.UserQuery;
import com.example.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param map
     * @return
     */
    @RequestMapping("/login")

    public Result login(@RequestBody Map<String,String> map){

        String username=map.get("name");
        String password=map.get("password");
        return userService.login(username,password);

    }

    /**
     * 查询
     * @param userQuery
     * @return
     */
    @RequestMapping("/getUserList")
    public Result getUserList(@RequestBody UserQuery userQuery){


        PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());

        List<User> list=userService.getUserList(userQuery.getRealname());
       // System.err.println(list);

        PageInfo<User> pageInfo=new PageInfo<>(list);

        Map<String,Object> resultMap=new HashMap<>();

        //当前页数据
        resultMap.put("list",pageInfo.getList());

        //数据总数
        resultMap.put("total",pageInfo.getTotal());

        return new Result(200,"数据加载成功",resultMap);

    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public Result addUserInformation(@RequestBody User user){

        return userService.addUserInformation(user);

    }

    /**
     * 修改
     * @param user
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user){
        return userService.edit(user);
    }

    @RequestMapping("/delUserById")
    public Result delUserById(@RequestBody Map<String,Integer> map){

        int id =map.get("id");

        return userService.delUserById(id);
    }

    /**
     *
     * @param map
     * @return
     */
    @RequestMapping("/delBatch")
    public Result delBatch(@RequestBody Map<String,String> map){

        String ids = map.get("ids");

        return userService.delBatch(ids);
    }
}
